﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ArgusLib.Drawing
{
	public struct RectangleA
	{
		SizeF size;
		public SizeF Size { get { return this.size; } set { this.size = value; } }
		PointF center;
		public PointF Center { get { return this.center; } set { this.center = value; } }
		public float Rotation { get; set; }

		public float Width { get { return this.size.Width; } set { this.size.Width = value; } }
		public float Height { get { return this.size.Height; } set { this.size.Height = value; } }
		public float X { get { return this.center.X; } set { this.center.X = value; } }
		public float Y { get { return this.center.Y; } set { this.center.Y = value; } }
		public float DiagonalDistance{get{return (float)Math.Sqrt(this.Width*this.Width+this.Height*this.Height);}}

		public RectangleA(PointF center, SizeF size, float rotation)
			: this()
		{
			this.center = center;
			this.size = size;
			this.Rotation = rotation;
		}

		public RectangleA(float x, float y, float width, float height, float rotation)
			: this()
		{
			this.center = new PointF(x, y);
			this.size = new SizeF(width, height);
			this.Rotation = rotation;
		}

		public PointF TopLeft
		{
			get
			{
				double angle = Math.Atan2(-this.Height, -this.Width) + this.Rotation;
				double x = this.X + this.DiagonalDistance / 2 * Math.Cos(angle);
				double y = this.Y + this.DiagonalDistance / 2 * Math.Sin(angle);
				return new PointF((float)x, (float)y);
			}
		}

		public PointF TopRight
		{
			get
			{
				double angle = Math.Atan2(-this.Height, this.Width) + this.Rotation;
				double x = this.X + this.DiagonalDistance / 2 * Math.Cos(angle);
				double y = this.Y + this.DiagonalDistance / 2 * Math.Sin(angle);
				return new PointF((float)x, (float)y);
			}
		}

		public PointF BottomLeft
		{
			get
			{
				double angle = Math.Atan2(this.Height, -this.Width) + this.Rotation;
				double x = this.X + this.DiagonalDistance / 2 * Math.Cos(angle);
				double y = this.Y + this.DiagonalDistance / 2 * Math.Sin(angle);
				return new PointF((float)x, (float)y);
			}
		}

		public PointF BottomRight
		{
			get
			{
				double angle = Math.Atan2(this.Height, this.Width) + this.Rotation;
				double x = this.X + this.DiagonalDistance / 2 * Math.Cos(angle);
				double y = this.Y + this.DiagonalDistance / 2 * Math.Sin(angle);
				return new PointF((float)x, (float)y);
			}
		}

		public static implicit operator RectangleA(RectangleF rect)
		{
			return new RectangleA(rect.X + rect.Width / 2, rect.Y + rect.Height / 2, rect.Width, rect.Height, 0);
		}
	}
}
